package com.lcxw.hxzbapi.scheduleTask;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lcxw.hxzbapi.entity.pojo.IncomeTotal;
import com.lcxw.hxzbapi.entity.pojo.IncomeUnion;
import com.lcxw.hxzbapi.entity.pojo.IncomeUnionDetail;
import com.lcxw.hxzbapi.entity.pojo.Union;
import com.lcxw.hxzbapi.entity.pojo.UnionMember;
import com.lcxw.hxzbapi.service.IncomeTotalService;
import com.lcxw.hxzbapi.service.IncomeUnionDetailService;
import com.lcxw.hxzbapi.service.IncomeUnionService;
import com.lcxw.hxzbapi.service.UnionMemberService;
import com.lcxw.hxzbapi.service.UnionService;
import com.lcxw.hxzbcommon.common.StringUtil;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UnionIncomeScheduled {

    @Resource
    UnionMemberService unionMemberService;

    @Resource
    UnionService unionService;

    @Resource
    IncomeTotalService incomeTotalService;

    @Resource
    IncomeUnionService incomeUnionService;

    @Resource
    IncomeUnionDetailService incomeUnionDetailService;

    //    @Scheduled(cron = "0/30 * * * * *")
    @Scheduled(cron = "0 0 3 ? * 2")
    void countUnionIncome() {

        String term = StringUtil.getDateTime().split(" ")[0].replace("-", "");

        List<Union> allUnions = unionService.getAll();
        for (Union union : allUnions
        ) {
            int unionId = union.getUnionId();
            double unionIncome = 0.00;
            List<UnionMember> allMembers = unionMemberService.getAllMembers(unionId);
            for (UnionMember unionMember : allMembers
            ) {
                Long userId = unionMember.getUserId();
                IncomeTotal incomeTotal = incomeTotalService.selectByUserId(userId);
                if (incomeTotal != null && incomeTotal.getIncomeToUnion() > 0) {

                    Double incomeToUnion = incomeTotal.getIncomeToUnion();
                    unionIncome += incomeToUnion;

                    IncomeUnionDetail incomeUnionDetail = new IncomeUnionDetail();
                    incomeUnionDetail.setUnionId(unionId);
                    incomeUnionDetail.setUserId(userId);
                    incomeUnionDetail.setTerm(term);
                    incomeUnionDetail.setIncome(incomeToUnion);
                    incomeUnionDetail.setStatus("0");
                    incomeUnionDetail.setIncomeProp(
                        StringUtil.formatDouble(incomeToUnion * union.getProportion()));
                    incomeUnionDetailService.insert(incomeUnionDetail);

                    incomeTotal.setIncomeToUnion(0.00);
                    incomeTotalService.update(incomeTotal);
                }
            }

            IncomeUnion incomeUnion = new IncomeUnion();
            incomeUnion.setUnionId(unionId);
            incomeUnion.setIncome(unionIncome);
            incomeUnion.setStatus("0");
            incomeUnion.setTerm(term);

            incomeUnionService.insert(incomeUnion);
        }
    }

}
